package com.itcast.service;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;

import com.itcast.dao.MemBerMobileMapper;
import com.itcast.dao.OrderMobileMapper;
import com.itcast.dao.OrderSettingMapper;
import com.itcast.health.constant.MessageConstant;
import com.itcast.health.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Wang
 * @version v1.0
 * @date 2019/11/28 10:19
 * @description TODO
 **/
@Service(interfaceClass = OrderMobileService.class)
@Transactional
public class OrderMobileServiceImpl implements OrderMobileService {


    @Autowired
    OrderSettingMapper orderSettingMapper;
    @Autowired
    OrderMobileMapper orderMobileMapper;
    @Autowired
    MemBerMobileMapper memBerMobileMapper;

    @Override
    public Result createOrder(OrderInfoVo orderInfoVo) {
        Date orderDate = orderInfoVo.getOrderDate();
        String telephone = orderInfoVo.getTelephone();
        String idCard = orderInfoVo.getIdCard();
        String name = orderInfoVo.getName();
        Integer sex = orderInfoVo.getSex();
        Integer setmealId = orderInfoVo.getSetmealId();
        String orderType = orderInfoVo.getOrderType();
        //根据预约的时间查询是否存在预约设置(返回一个预约设置对象)
        OrderSetting orderSetting = orderSettingMapper.findByOrderDate(orderDate);
        if (orderSetting == null) {
            return Result.error("没有档期");
        }
        //判断是否预约满
        int reservations = orderSetting.getReservations();
        int number = orderSetting.getNumber();
        if (number <= reservations) {
            return Result.error(MessageConstant.ORDER_FULL);
        }
        //根据手机号查询此用户是否为会员(返回一个会员对象)
        Member member = memBerMobileMapper.findByTelephone(telephone);
        //如果不是会员就自动注册(返回一个会员id)
        if (member == null) {
            member = new Member();//空对象需要初始化
            member.setName(name);
            member.setIdCard(idCard);
            member.setPhoneNumber(telephone);
            member.setSex(String.valueOf(sex));
            member.setRegTime(new Date());
            memBerMobileMapper.add(member);
        }
        Integer memberId = member.getId();
        //根据会员id,套餐id,预约时间查询是否存在预约信息
        List<Order> orderList = orderMobileMapper.findBySetmealIdAndMemBerIdAndOrderDate(setmealId, memberId, orderDate);
        //如果orderList不为空 说明已经预约,提示用户
        if (CollectionUtil.isNotEmpty(orderList)) {
            return Result.error("请不要重复预约");
        }
        //下单
        Order order = new Order();
        order.setMemberId(memberId);
        order.setOrderDate(orderDate);
        order.setOrderType(orderType);
        order.setSetmealId(setmealId);
        orderMobileMapper.add(order);
        //预约设置中已预约人数+1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
         int i=orderSettingMapper.upDateReservations(orderSetting);
         if (i==0){
             //没有更新成功
             throw new RuntimeException("");
         }
        //返回预约成功
        return Result.success("", order);
    }


    /**
     * 预约成功后回显数据
     */
    @Override
    public Map findById(Integer id) {
        return orderMobileMapper.findById(id);
    }
}